package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.entity.Banner;
import com.vincent.kdp.ui.commodity.CommodityListActivity;

import java.util.List;


/**
 * Created by Administrator on 2016/5/31.
 */
public class BannerViewPagerAdapter extends PagerAdapter {
    private List<Banner> bannerList;
    private Context mContext;

    public BannerViewPagerAdapter(Context context, List<Banner> bannerList) {
        this.bannerList = bannerList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        object = null;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        if(bannerList.size()>0){
            Glide.with(mContext).load(bannerList.get(position % bannerList.size()).getItemImgUrl()).placeholder(R.mipmap
                    .ic_default_image).into(imageView);
        }

        container.addView(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bannerList.size()>0){
                    SPUtil.put("seriesId",bannerList.get(position % bannerList.size()).getItemScroId());
                    SPUtil.put("seriesName",bannerList.get(position % bannerList.size()).getItemName());
                    mContext.startActivity(new Intent(mContext, CommodityListActivity.class));
                }
            }
        });


        return imageView;
    }
}
